/*******************************************************************************
 * Copyright (c) 2008-2009 zhang yuexiang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package org.xfeep.asura.bootstrap;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xfeep.asura.core.ComponentDefinition;
import org.xfeep.asura.core.ComponentManager;
import org.xfeep.asura.core.logger.CoreLoggerManager;
import org.xfeep.asura.core.logger.LoggerService;
import org.xfeep.asura.core.logger.TinyLogService;

/**
 * SimpleComponentLoader can load all component definitations from a single simple text file where one line declares one 
 * component class name. The java class must be annotated with annotation @Service otherwise it will be be ignored silently.  
 * @author zhang yuexiang
 *
 */
public class SimpleComponentLoader implements ComponentLoader {

	public final static String COMPONENT_LIST = "asura_component_list";
	
	ComponentManager componentManager;
	ComponentDynamicLoadListener listener;
	LoggerService logService = CoreLoggerManager.getDefaultLogger();
	InputStream txtStream;
	String listFile;
	boolean streamExternal = true;
	
	public SimpleComponentLoader() throws FileNotFoundException {
		this(getComponentListFile());
	}
	
	public SimpleComponentLoader(String txtFile) throws FileNotFoundException {
		this(new FileInputStream(txtFile));
		listFile = txtFile;
		streamExternal = false;
	}
	
	public SimpleComponentLoader(URL txtURL) throws IOException {
		this.txtStream = txtURL.openStream();
		listFile = txtURL.toString();
		streamExternal = false;
	}
	
	public SimpleComponentLoader(InputStream txtStream) {
		this.txtStream = txtStream;
	}
	
	public static String getComponentListFile() {
		String listFile = System.getProperty(COMPONENT_LIST);
		if (listFile == null){
			listFile = COMPONENT_LIST;
		}
		return listFile;
	}
	
	
	public List<ComponentDefinition> getStaticLoadComponents(
			ClassLoader classLoader) throws IOException {
		List<ComponentDefinition> rt = new ArrayList<ComponentDefinition>();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(txtStream));
			String line = null;
			while (  (line = reader.readLine()) != null ){
				line = line.trim();
				if (!line.startsWith("#") && line.length() > 0){
					try {
						rt.add(ComponentDefinition.create(classLoader.loadClass(line)));
					} catch (ClassNotFoundException e) {
						logService.warn("can not load component: " + line + ", just skip it" , e);
					}
				}
			}
		} catch (FileNotFoundException e) {
			logService.error("can not load component list file: " + listFile, e);
		} catch (IOException e) {
			logService.error("can not read component list file: " + listFile, e);
		}finally{
			if (!streamExternal){
				txtStream.close();
			}
		}
		return rt;
	}

	public void initialize(ComponentManager componentManager) {
		this.componentManager = componentManager;
	}

	public void setupComponentDynamicLoadListener(
			ComponentDynamicLoadListener listener) {
		this.listener = listener;
	}
	
	@Override
	public String toString() {
		if (listFile != null){
			return this.getClass().getSimpleName() + ": " + listFile;
		}
		return this.getClass().getName();
	}

}
